package com.itheima.edu.list;

import java.lang.reflect.Array;
import java.util.*;

public class StudentAdmin {
    private static Scanner sc = new Scanner(System.in);
    private static ArrayList<Student> stuList = new ArrayList<>();
    static {
        Student stu1 = new Student("2020111101001", "张三", 24);
        Student stu2 = new Student("2020111101002", "李四", 23);
        Student stu3 = new Student("2020111101003", "王五", 25);
        stuList.add(stu1);
        stuList.add(stu2);
        stuList.add(stu3);
    }

    private static ArrayList<Student1> stuList1 = new ArrayList<>();
    static {
        Student1 stu01 = new Student1("2020111101001", "zhangsan", 24);
        Student1 stu02 = new Student1("2020111101002", "lisi", 23);
        Student1 stu03 = new Student1("2020111101003", "wwangwu", 25);
        Student1 stu04 = new Student1("2020111101004", "zhaoliu", 25);
        stuList1.add(stu01);
        stuList1.add(stu02);
        stuList1.add(stu03);
        stuList1.add(stu04);
    }

    public static void main(String[] args) {
        System.out.println("---------------欢迎来到学生管理系统！---------------");

        while (true) {
            System.out.println("请输入您要进行的操作：");
            System.out.println("1、添加学生\t2、删除学生\t3、修改学生\t4、查看学生\t5、根据学生年龄进行排序\t999、退出本系统");
            int choice = sc.nextInt();

            switch (choice) {
                case 1:
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case 2:
                    //System.out.println("删除学生");
                    deleteStudent();
                    break;
                case 3:
                    //System.out.println("修改学生");
                    updateStudent();
                    break;
                case 4:
                    //System.out.println("查看学生");
                    printStudent();
                    break;
                case 5:
                    //System.out.println("根据学生年龄进行排序(暂时未实现)");
                    shortStudent();
                    break;
                case 999:
                    System.out.println("系统退出！感谢您的使用！");
                    System.exit(0);
            }
        }
    }

    private static void shortStudent() {
        //自然排序： 1、Student需要实现Compatable接口
        //         2、Student中需要重写compareTo方法
        /*TreeSet<Student1> stuTs1 = new TreeSet<>();
        for (int i = 0; i < stuList1.size(); i++) {
            stuTs1.add(stuList1.get(i));
        }
        printStudent(stuTs1);*/

        //指定比较器进行排序
        TreeSet<Student> stuTs2 = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result = o1.getAge() - o2.getAge();
                result = result == 0 ? o1.getId().compareTo(o2.getId()) : result;
                result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
                return result;
            }
        });

        Student stu1 = new Student("2020111101001", "张三", 24);
        Student stu2 = new Student("2020111101002", "李四", 23);
        Student stu3 = new Student("2020111101003", "王五", 25);
        stuTs2.add(stu1);
        stuTs2.add(stu2);
        stuTs2.add(stu3);

        for (Student student : stuTs2) {
            System.out.println(student);
        }
    }

    private static void printStudent(Collection<Student1> cc) {
        if (cc.size() > 0) {
            //普通for循环遍历
            /*for (int i = 0; i < stuList.size(); i++) {
                Student stu = stuList.get(i);
                System.out.println(stu.toString());
            }*/

            //迭代器：集合特有的遍历方式
            /*Iterator<Student> newStuList = stuList.iterator();
            while(newStuList.hasNext()){
                System.out.println(newStuList.next());
            }*/

            //增强for
            for (Student1 student1 : cc) {
                System.out.println(student1);
            }
        }
    }

    private static void printStudent() {
        if (stuList.size() > 0) {
            System.out.println("Id\t\t\t\t\t姓名\t\t年龄");
            //普通for循环遍历
            /*for (int i = 0; i < stuList.size(); i++) {
                Student stu = stuList.get(i);
                System.out.println(stu.toString());
            }*/

            //迭代器：集合特有的遍历方式
            /*Iterator<Student> newStuList = stuList.iterator();
            while(newStuList.hasNext()){
                System.out.println(newStuList.next());
            }*/

            //增强for
            for (Student student : stuList) {
                System.out.println(student);
            }
        } else {
            System.out.println("查无学生，请添加后重试！");
        }

    }

    private static void updateStudent() {
        String id;
        while (true) {
            System.out.println("请输入需要修改的学生id：");
            id = sc.next();
            boolean result = isExsits(id);
            if (result) {
                break;
            } else {
                System.out.println("查无此人，请核对后重新输入！");
            }
        }

        for (int i = 0; i < stuList.size(); i++) {
            Student stu = stuList.get(i);
            if (id.equals(stu.getId())) {
                System.out.println("请输入姓名：");
                String name = sc.next();
                System.out.println("请输入年龄：");
                int age = sc.nextInt();

                Student newStu = new Student(id, name, age);

                stuList.set(i, newStu);
                System.out.println("修改成功！");
            }
        }
    }

    private static void deleteStudent() {
        System.out.println("请输入需要删除的学生id：");
        String id = sc.next();
        for (int i = 0; i < stuList.size(); i++) {
            Student stu = stuList.get(i);
            if (id.equals(stu.getId())) {
                stuList.remove(i);
                System.out.println("删除成功！");
                return;
            }
        }
    }

    private static void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入学生ID：");
            System.out.println("id格式为：入学年月日+女生为00，男生为01+自己的三位数编号");
            id = sc.next();
            boolean result = isExsits(id);
            if (result) {
                System.out.println("您输入的学号已经存在！请重新输入！");
            } else {
                break;
            }
        }
        System.out.println("请输入姓名：");
        String name = sc.next();
        System.out.println("请输入年龄：");
        int age = sc.nextInt();

        Student stu = new Student(id, name, age);

        stuList.add(stu);

        System.out.println("添加成功！");
    }

    private static boolean isExsits(String id) {
        for (int i = 0; i < stuList.size(); i++) {
            Student stu = new Student();
            if (id.equals(stu.getId())) {
                return true;
            }
        }
        return false;
    }
}
